package com.ruoyi.web.controller.ai;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.ai.domain.AIApiKey;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.service.IAIApiKeyService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * apiKey管理Controller
 *
 * @author gdk
 * @date 2023-05-29
 */
@RestController
@RequestMapping("/ai/api_key")
@AllArgsConstructor
public class AIApiKeyController extends BaseController {
    private final IAIApiKeyService aIApiKeyService;

    /**
     * 查询apiKey管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:api_key:list')")
    @GetMapping("/list")
    public TableDataInfo list(AIApiKey aIApiKey) {
        startPage();
        List<AIApiKey> list = aIApiKeyService.selectAIApiKeyList(aIApiKey);
        return getDataTable(list);
    }

    /**
     * 导出apiKey管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:api_key:export')")
    @Log(title = "apiKey管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AIApiKey aIApiKey) {
        List<AIApiKey> list = aIApiKeyService.selectAIApiKeyList(aIApiKey);
        ExcelUtil<AIApiKey> util = new ExcelUtil<AIApiKey>(AIApiKey.class);
        util.exportExcel(response, list, "apiKey管理数据");
    }

    /**
     * 获取apiKey管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ai:api_key:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aIApiKeyService.selectAIApiKeyById(id));
    }

    /**
     * 新增
     *
     * @param entity entity
     * @return {@link AjaxResult}
     * @author gdk
     * @since 2023/5/29 19:39
     */
    @PreAuthorize("@ss.hasPermi('ai:api_key:add')")
    @Log(title = "apiKey管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AIApiKey entity) {
        aIApiKeyService.insertAIApiKey(entity);
        return success();
    }

    /**
     * 逻辑删除
     *
     * @param ids ids
     * @return {@link AjaxResult}
     * @author gdk
     * @since 2023/5/29 20:03
     */
    @PreAuthorize("@ss.hasPermi('ai:api_key:remove')")
    @Log(title = "apiKey管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        aIApiKeyService.removeLogicByIds(ids);
        return success();
    }
}
